import java.util.*;

public class 前K个高频元素 {
    public int[] topKFrequent(int[] nums, int k) {
        int[] ans = new int[k];
        int cur = 0;
        Map<Integer,Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0) + 1);
        }
        PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> {
            return b[1] - a[1];
        });
        for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
            heap.add(new int[]{entry.getKey(),entry.getValue()});
        }
        while (k-- > 0) {
            ans[cur++] = heap.poll()[0];
        }
        return ans;
    }
}
